package com.pony.controller.admin;


import com.pony.dto.PageDTO;
import com.pony.dto.TypeDTO;
import com.pony.enums.AppHttpCodeEnum;
import com.pony.result.R;
import com.pony.service.ITypeService;
import com.pony.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author pony
 * @since 2023-11-16
 */
@Slf4j
@RestController
@RequestMapping("/admin/type")
@CrossOrigin(origins = "*")
public class TypeController {
    @Autowired
    private ITypeService typeService;

    /**
     * 添加类别
     * @param typeDTO
     * @return
     */
    @PostMapping
    public R addType(@RequestBody TypeDTO typeDTO) {
        log.info("添加的分类："+typeDTO.toString());
        // 参数校验
        if (StringUtils.isBlank(typeDTO.getName())){
            return R.error(AppHttpCodeEnum.PARAMETER_LACK);
        }
        return typeService.addType(typeDTO);
    }

    /**
     * 获取分类分页信息
     * @param pageDTO
     * @return
     */
    @PostMapping("/page")
    public R getPage(@RequestBody PageDTO pageDTO){
        log.info("分页信息"+pageDTO.toString());
        return typeService.getPage(pageDTO);
    }

    /**
     * 更改分类状态
     * @param id
     * @param status
     * @return
     */
    @GetMapping("/status/{id}/{status}")
    public void setStatus(@PathVariable("id") Integer id, @PathVariable("status") Integer status){
        typeService.setStatus(id, status);
    }

    /**
     * 根据分类id，查询单个分类信息
     * @param id
     * @return
     */
    @GetMapping("/selectOne/{id}")
    public R selectOneById(@PathVariable("id") Integer id){
        return typeService.selectOneById(id);
    }

    /**
     * 修改数据
     * @param typeDTO
     * @return
     */
    @PutMapping
    public R updateSinger(@RequestBody TypeDTO typeDTO){
        // 参数校验
        if (StringUtils.isBlank(typeDTO.getName())){
            return R.error(AppHttpCodeEnum.PARAMETER_LACK);
        }
        return typeService.updateSinger(typeDTO);
    }

    /**
     * 根据id删除分类
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R deleteById(@PathVariable("id") Integer id) {
        return typeService.deleteOneById(id);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("deleteBatch")
    public R deleteBatch(@RequestBody List<Integer> ids){
        log.info(ids.toString());
        if(ids == null || ids.size() ==0 ){
            return R.error(AppHttpCodeEnum.PARAMETER_LACK);
        }
        return typeService.deleteBatch(ids);
    }

    /**
     * 查询所有分类信息
     * @return
     */
    @GetMapping("/getAll")
    public R selectAll(){
        return typeService.selectAll();
    }
}
